package com.liang.lingdong.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liang.lingdong.common.BaseResponse;
import com.liang.lingdong.common.ErrorCode;
import com.liang.lingdong.common.ResultUtils;
import com.liang.lingdong.enums.UserRoleEnum;
import com.liang.lingdong.exception.BusinessException;
import com.liang.lingdong.model.user.dto.ForgetPasswordDto;
import com.liang.lingdong.model.user.dto.QueryUserListDto;
import com.liang.lingdong.model.user.dto.UpdatePasswordDto;
import com.liang.lingdong.model.user.vo.UserVo;
import com.liang.lingdong.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 分页查询用户列表
     * @param queryUserListDto 查询条件
     * @return 结果
     */
    @PostMapping("/page")
    public BaseResponse<IPage<UserVo>> page(@RequestBody QueryUserListDto queryUserListDto) {
        if (queryUserListDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        IPage<UserVo> userVoIPage = userService.queryUserPage(queryUserListDto);
        return ResultUtils.success(userVoIPage);
    }

    /**
     * 用户授权
     * @param businessId id
     * @return 结果
     */
    @PutMapping("/auth")
    public BaseResponse<Boolean> auth(@RequestBody Long businessId) {
        if (businessId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean flag = userService.authUser(businessId);
        return ResultUtils.success(flag);
    }

    /**
     * 查询单一用户
     */
    @GetMapping("/query")
    public BaseResponse<UserVo> query(@RequestParam Long businessId) {
        if (businessId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVo userVo = userService.getUserInfo(businessId);
        return ResultUtils.success(userVo);
    }

    /**
     * 修改密码
     */
    @PutMapping("/update/password")
    public BaseResponse<Boolean> updatePassword(@RequestBody @Validated UpdatePasswordDto updatePasswordDto) {
        if (updatePasswordDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean flag = userService.updatePassword(updatePasswordDto);
        return ResultUtils.success(flag);
    }

    /**
     * 忘记密码
     */
    @PutMapping("/forget/password")
    public BaseResponse<Boolean> forgetPassword(@RequestBody @Validated ForgetPasswordDto forgetPasswordDto) {
        if (forgetPasswordDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean flag = userService.forgetPassword(forgetPasswordDto);
        return ResultUtils.success(flag);
    }
}
